package com.hjxy.codetools.controller;

import com.alipay.api.internal.util.StringUtils;
import com.hjxy.codetools.dto.PayOrderDto;
import com.hjxy.codetools.interfa.PayOrderServiceInterface;
import com.hjxy.codetools.model.PayOrderModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "api/payorder")
public class RestPayOrderController {

    @Autowired
    PayOrderServiceInterface payOrderServiceInterface;
    /**
     * 给网站使用
     * 根据订单号
     * 查询详情
     * @param queryItem
     * @return
     * @throws Exception
     */
    @PostMapping(value = "getOrderByCode")
    public Map<String, Object> getOrderByCode(@RequestBody Map<String, String> queryItem) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        List<PayOrderModel> payOrderModels= payOrderServiceInterface.getPayOrderInfo(queryItem);
        if (payOrderModels!=null&&payOrderModels.size()>0)
            resultMap.put("code", 20000);
        else
            resultMap.put("code", 40400);
        return resultMap;
    }

    /**
     * 给后台使用
     * 查询订单列表
     * 分页
     * @param queryItem
     * @return
     * @throws Exception
     */
    @PostMapping(value = "getOrdersInfo")
    public Map<String, Object> getOrdersInfo(@RequestBody Map<String, String> queryItem) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        int pageIndex = Integer.parseInt(queryItem.get("pageIndex"));
        int pageSize = Integer.parseInt(queryItem.get("pageSize"));
        String payresult = queryItem.get("payresult");
        String tname=queryItem.get("tname");//将后台ui前端查询条件 tname 和tel 合并
        String starttime=queryItem.get("starttime");
        String endtime=queryItem.get("endtime");
        Map whMap=new HashMap();
        whMap.put("start",(pageIndex - 1) * pageSize);
        whMap.put("end", pageSize);
        if (!StringUtils.isEmpty(starttime)){

            Long time =Long.parseLong(starttime);
            //转化Date
            java.util.Date dataTime = new java.util.Date(time);

            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            starttime= dateFormat.format(dataTime);
            whMap.put("starttime", starttime);
        }
        if (!StringUtils.isEmpty(endtime)){

            Long time =Long.parseLong(endtime);
            //转化Date
            java.util.Date dataTime = new java.util.Date(time);

            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            endtime= dateFormat.format(dataTime);
            whMap.put("endtime", endtime);
        }

//        if (!"2".equals(payresult))
//         whMap.put("payresult", payresult);
        whMap.put("payresult", 1);//固定查询已经支付完成的订单
        whMap.put("tname", tname);

        List<PayOrderDto> payOrderDtos = payOrderServiceInterface.getOrdersInfo(whMap);
        int totalNum = payOrderServiceInterface.getTotalNum(whMap);
        int totalPayNum = payOrderServiceInterface.getTotalPayNum(whMap);

        resultMap.put("data", payOrderDtos);
        resultMap.put("code", 20000);
        resultMap.put("totalPageNum",totalNum);
        resultMap.put("totalPayNum",totalPayNum);
        return resultMap;

    }
}
